package ua.ck.example.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import ua.ck.example.domain.Category;
import ua.ck.example.domain.Product;
import ua.ck.example.domain.Provider;
import ua.ck.example.service.CategoryService;
import ua.ck.example.service.ProductService;
import ua.ck.example.util.Constant;

@Controller
@RequestMapping(ProductController.URL)
public class ProductController {
	static final String URL = "/product";

	@Autowired
	private ProductService productService;
	@Autowired
	private CategoryService categoryService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String listProducts(
			Model model,
			@RequestParam(value = "productName", required = false) String productName) {
		Product product = new Product();
		product.setProductName(productName);
		model.addAttribute("listProducts", productService.getProducts(product));
		return "product/index";

	}

	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String showProduct(Model model,
			@RequestParam(value = "productId", required = false) int productId) {

		Product product = new Product();
		product.setProductId(productId);
		model.addAttribute("specifyProduct", productService
				.getProducts(product).get(0));
		return "product/show";

	}

	@RequestMapping(value = "/mine", method = RequestMethod.GET)
	public String mineProducts(Model model, HttpServletRequest req) {
		Provider provider = (Provider) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER);
		Product productParam = new Product();
		productParam.setProvider(provider);
		model.addAttribute("mineProducts",
				productService.getProducts(productParam));
		return "product/mine";

	}

	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public String delProducts(
			Model model,
			@RequestParam(value = "productId", required = false) int productId,
			@RequestParam(value = "categoryId", required = false) int categoryId,
			HttpServletRequest req) {
		Product product = new Product();
		product.setProductId(productId);
		Provider provider = (Provider) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER);
		product.setProvider(provider);

		Category category = new Category();
		category.setCategoryId(categoryId);
		product.setCategory(category);
		try {
			productService.delProducts(product);
			provider.getProducts().remove(product);
		} catch (Exception e) {
            return "product/nodel";
		}
		return "redirect:" + URL + "/mine";

	}

	// http://localhost:8080/e-shop-example/product/addproduct
	@RequestMapping(value = "/addproduct", method = RequestMethod.GET)
	public String addProduct(Model model) {
		Product product = new Product();
		List<Category> categoryList = categoryService.listCategories();
		// List<String> cats = new ArrayList<String>();
		// for (Category cat : categoryList) {
		// cats.add(cat.getCategoryName());
		// }

		model.addAttribute("product", new Product());
		model.addAttribute("cats", categoryList);
		return "product/addproduct";
	}

	@RequestMapping(value = "/addproduct", method = RequestMethod.POST)
	public String addProduct(@ModelAttribute("product") Product product,
			BindingResult result, HttpServletRequest req) {
		Category cat = new Category();
		cat.setCategoryId(product.getCategoryId());
		product.setCategory(cat);
		Provider provider = (Provider) req.getSession().getAttribute(
				Constant.SESSION_KEY_USER);
		if (provider != null) {
			product.setProvider(provider);
		}
		provider.getProducts().add(product);
		productService.addProduct(product);
		return "redirect:" + URL + "/index";

	}
}
